<?php
class Ticket extends Controller {
	function Ticket() {
		parent::Controller();
		$this->load->helper("url");
		$this->data["base"] = $this->config->item('base_url');
		$this->load->model('Ticketmodel','',TRUE);
		$this->load->model('TransLog');
		
		$this->load->database();	
                session_start();
		
		
	}
	function index() {
		if(((isset($_SESSION['sys_admin'])) and isset($_SESSION['name']))||((isset($_SESSION['admin']))and isset($_SESSION['name']))||((isset($_SESSION['intern_audit']))and isset($_SESSION['name']))||((isset($_SESSION['second_consult']))and isset($_SESSION['name']))||((isset($_SESSION['dept_pic']))and isset($_SESSION['name']))) {
			$this->ticket_1();
		} else {
			redirect('main');
		}
		
	}
	function get_value($id=''){
		$select="";
		for($i=0;$i<=20;$i++){
			$select .="<input type='text' value='$i'>";
		}
		die($select);
		/*$a ="";
		if(isset($id) =='') {
			$a =.="<input type='text' value='coba'>";
		}*/
	}
	function show_id() {
		$this->data['h2_title'] = 'CREATE TICKET';
		$this->data['form_action'] = site_url('createticket/ticket/ticket_1');
		$id = $this->Ticketmodel->show_id()->row();
		$this->data['id'] = $id->ticket_id + 1;
		$name =$_SESSION['name'];
		$idx = $this->Ticketmodel->show_user_id($name)->row();
		$this->data['name'] = $idx->name;
		$this->data['id_user'] =$idx->user_id;
		$user_id = $idx->user_id;
		$x = $this->Ticketmodel->show_reference($user_id)->row();
		$this->data['ref'] = isset($x->ticket_id);
		$this->data['prod'] = $this->Ticketmodel->show_product();
				
		$this->load->vars($this->data);
		$this->load->view('layout/head');
		$this->load->view('layout/body');
		$this->load->view('layout/search');
		$this->load->view('ticket/ticket');
		$this->load->view('layout/foot');
	}
	function ticket_1() {
		$this->data['titles']='Create Ticket';
		$this->data['h2_title'] = '';
		$this->data['form_action'] = site_url('createticket/ticket/ticket_1');
		//--------------------
		$id = $this->Ticketmodel->show_id()->row();
		$this->data['id'] = $id->ticket_id + 1;
		$name =$_SESSION['name'];
		$idx = $this->Ticketmodel->show_user_id($name)->row();
		$this->data['name'] = $idx->name;
		$this->data['id_user'] =$idx->user_id;
		$user_id = $idx->user_id;
		$x = $this->Ticketmodel->show_reference($user_id)->result();
		foreach ($x as $y) {
		$this->data['ref'] = $y->ticket_id;
		}
		//--------------------
		
		$this->data['prod'] = $this->Ticketmodel->show_product();
		$id = $this->input->post('ticket_id');
		$x = $this->Ticketmodel->ubah_date($id)->row();
		$this->data['change'] =isset( $x->ticked_id);
		$this->data['change2'] = $this->input->post('ticked_id');
		
		/* Coment2
		--------------------------------------------------------------------------*/
			$comment2 = $this->Ticketmodel->show_id_coments2()->row();
			$this->data['idcmnt2'] = $comment2->id + 1;
		/* End Coment2
		--------------------------------------------------------------------------*/
		
		
		
		//set template
		$this->data['ticket_id'] = $this->input->post('ticket_id');
		$this->data['title'] = $this->input->post('title');
		$this->data['reference'] = $this->input->post('reference_id');
		$this->data['vul_det'] = $this->input->post('vul_detail');
		$this->data['vul_sol'] = $this->input->post('sol_detail');
		$this->data['impact'] = $this->input->post('impact_detail');
		$this->data['open_date'] = $this->input->post('open_datetime');
		$this->data['prob'] = $this->input->post('prob_o_impact');
		$this->data['level'] = $this->input->post('level_o_impact');
		$this->data['status'] = $this->input->post('status');
		$this->data['user_id'] = $this->input->post('user_id');
		//$this->data['name'] = $this->input->post('user_id2');
		$this->data['coments']= $this->input->post('notes');
		$this->data['product'] = $this->input->post('prod_id');
		$this->data['coment_id'] =$this->input->get('ticket');
		// menentukan nilai severity of level
		if(($this->input->post('prob_o_impact') * $this->input->post('level_o_impact')) == 9){
			$this->data['severity'] = 'Critical';
		} else if(($this->input->post('prob_o_impact') * $this->input->post('level_o_impact')) == 6) {
			$this->data['severity'] = 'High';
		} else if(($this->input->post('prob_o_impact') * $this->input->post('level_o_impact')) == 4) {
			$this->data['severity'] = 'Medium';
		} else if(($this->input->post('prob_o_impact') * $this->input->post('level_o_impact')) == 3) {
			$this->data['severity'] = 'Medium';
		} else if(($this->input->post('prob_o_impact') * $this->input->post('level_o_impact')) == 2) {
			$this->data['severity'] = 'Low';
		} else if(($this->input->post('prob_o_impact') * $this->input->post('level_o_impact')) == 1) {
			$this->data['severity'] = 'Note';
		} else {
			$this->data['severity'] = '';
		}
		
/* CREATE TICKET
-------------------------------------------------------------------------------------*/	
		
		if(isset($_POST['create'])) {
			
						
			$number_of_file_fields = 0;
    		$number_of_uploaded_files = 0;
    		$number_of_moved_files = 0;
    		$uploaded_files = array();
    		$upload_directory = './uploads/'; //set upload directory
    		
			for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
				$number_of_file_fields++;
				if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
					$number_of_uploaded_files++;
					$uploaded_files[] = $_FILES['images']['name'][$i];
					if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
						$number_of_moved_files++;
						
					}
					
		 			
				}
			}
			
			//mengambil array image upload
					if($uploaded_files[] = $_FILES['images']['name'][0]) {
						$a =$uploaded_files[0];
					} else {
						$a ="";
					}
					if($uploaded_files[] = (isset($_FILES['images']['name'][1]))) {
						$b =$uploaded_files[1];
					}else {
						$b ="";
					}
					if($uploaded_files[] = (isset($_FILES['images']['name'][2]))) {
						$c =$uploaded_files[2];
					}else {
						$c="";
					}
					if($uploaded_files[] =(isset( $_FILES['images']['name'][3]))) {
						$d =$uploaded_files[3];
					}else {
						$d="";
					}
					if($uploaded_files[] =(isset($_FILES['images']['name'][4]))) {
						$e =$uploaded_files[4];
					}else {
						$e="";
					}
					if($uploaded_files[] =(isset( $_FILES['images']['name'][5]))) {
						$f =$uploaded_files[5];
					}else {
						$f="";
					}
					if($uploaded_files[] =(isset( $_FILES['images']['name'][6]))) {
						$g =$uploaded_files[6];
					}else {
						$g="";
					}
					if($uploaded_files[] =(isset($_FILES['images']['name'][7]))) {
						$h =$uploaded_files[7];
					}else {
						$h="";
					}
					if($uploaded_files[] =(isset($_FILES['images']['name'][8]))) {
						$ix =$uploaded_files[8];
					}else {
						$ix="";
					}
					if($uploaded_files[] =(isset($_FILES['images']['name'][9]))) {
						$j =$uploaded_files[9];
					}else {
						$j="";
					}		
			//$this->form_validation->set_rules('ticket_id','Ticket ID','required');
			//$this->form_validation->set_rules('reference_id','Reference ID','required');
			$this->form_validation->set_rules('title','Title','required');
			$this->form_validation->set_rules('vul_detail','Vul Detail','required');
			$this->form_validation->set_rules('sol_detail','Sol Detail','required');
			$this->form_validation->set_rules('user_id','User Id','required');
			$this->form_validation->set_rules('open_datetime','Open Datetime','required');
			$this->form_validation->set_rules('target_date','Target Date','required');
			//$this->form_validation->set_rules('close_date','Close Date','required');
			//$this->form_validation->set_rules('status','Status','required');
			
			$this->form_validation->set_rules('impact_detail','impact_detail','required');
			$this->form_validation->set_rules('level_o_impact','Level O Impact','required');
			$this->form_validation->set_rules('prob_o_impact','Prob O Impact','required');
			$this->form_validation->set_rules('prod_id','Product','required');
			$this->form_validation->set_rules('severity_level','Severity Level','required');
			//$this->form_validation->set_rules('notes','Notes','required');

				if($this->form_validation->run() == TRUE) {
					$ticket_insert = array('ticket_id' => $this->input->post('ticket_id'),
									'reference_id' =>$this->input->post('reference_id'),
									'title' =>strip_tags($this->input->post('title')),
									'vul_detail' =>strip_tags($this->input->post('vul_detail')),
									'sol_detail' =>strip_tags($this->input->post('sol_detail')),
									'impact_detail' =>strip_tags($this->input->post('impact_detail')),
									'user_id' =>$this->input->post('user_id'),
									'open_datetime' =>date('Y-m-d H:i',strtotime($this->input->post('open_datetime'))),
									'target_date' =>$this->input->post('target_date'),
									'close_date'=>$this->input->post('close_date'),
									'status' =>$this->input->post('status'),
									'level_o_impact' =>$this->input->post('level_o_impact'),
									'prob_o_impact' =>$this->input->post('prob_o_impact'),
									'severity_level' =>$this->input->post('severity_level'),
									'prod_id' =>$this->input->post('prod_id'),
									'images1' =>$a,
									'images2' =>$b,
									'images3' =>$c,
									'images4' =>$d,
									'images5' =>$e,
									'images6' =>$f,
									'images7' =>$g,
									'images8' =>$h,
									'images9' =>$ix,
									'images10' =>$j,
									'notes' =>$this->input->post(''));
			//inset risk ...
				$this->Ticketmodel->ticket_insert($ticket_insert);
				$this->session->set_flashdata('message', 'Create Ticket Sucsess... !');
				
			/* Insert Comments2
				------------------------------------------------------------------------------*/
				$idcmnt =array('id' =>$this->input->post('id'),
								'ticket_id' =>$this->input->post('ticket_id'),
								'user_id' =>$this->input->post('user_id'),
								'notes' =>$this->input->post('notes'),
								'tanggal' =>date('Y-m-d H:i',strtotime($this->input->post('tanggal'))),
								'images1' =>$a,
									'images2' =>$b,
									'images3' =>$c,
									'images4' =>$d,
									'images5' =>$e,
									'images6' =>$f,
									'images7' =>$g,
									'images8' =>$h,
									'images9' =>$ix,
									'images10' =>$j);
				$this->Ticketmodel->addComent2($idcmnt);
				
				
			/* end Insert Comments
			-------------------------------------------------------------------------------*/
			/* images
			--------------------------------------------------------------------------*/
			$s_id_images =$this->Ticketmodel->images_id()->row();
			$sh_image_id = $s_id_images->image_id + 1;
		
			/* end images
			-------------------------------------------------------------------------*/
					
			/* Insert Log
			------------------------------------------------------------------------------*/
			if(getenv("HTTP_CLIENT_IP")) { 
				$ipad = getenv("HTTP_CLIENT_IP"); 
			} elseif(getenv("HTTP_X_FORWARDED_FOR")) { 
				$ipad = getenv("HTTP_X_FORWARDED_FOR"); 
			} else { 
				$ipad = getenv("REMOTE_ADDR"); 
			}
				$ipad = substr($ipad, 0,14);
				$translog_id = $this->TransLog->show_id_trans_log()->row();
		
				$this->xd = $translog_id->id + 1;
		
		
				$this->send_id =$_SESSION['name'];
				$this->tgl = date('Y-m-d H:i:s');
				
				$insert_log = array('id' => $this->xd,
							'user_id' =>$this->send_id,
							'ticket_id' =>$this->input->post(''),
							'notes' =>('Create Ticket'),
							'datetime' =>$this->tgl,
							'ip' =>$ipad);
				//insert ke table trans log
				$this->TransLog->insert_log($insert_log);
				/* end Trnslog
				--------------------------------------------------------------------------------*/
		
				redirect('createticket/mngticket');
			}else {
			}
		} 
/* END CREATE TICKET
------------------------------------------------------------------------------------------------*/

/* CREATE COMENTS
-------------------------------------------------------------------------------------------------*/
		if(isset($_POST['save'])) {
			$number_of_file_fields = 0;
    		$number_of_uploaded_files = 0;
    		$number_of_moved_files = 0;
    		$uploaded_files = array();
    		$upload_directory = './uploads/coments/'; //set upload directory
    		
			for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
				$number_of_file_fields++;
				if ($_FILES['images']['name'][$i] != '') { //check if file field empty or not
					$number_of_uploaded_files++;
					$uploaded_files[] = $_FILES['images']['name'][$i];
					if (move_uploaded_file($_FILES['images']['tmp_name'][$i], $upload_directory . $_FILES['images']['name'][$i])) {
						$number_of_moved_files++;
						
					}
					
		 			
				}
			}
			// mengambil data array
			if($uploaded_files[] = (isset($_FILES['images']['name']['0']))) {
				$img1 = $uploaded_files[0];
			} else {
				$img1 ="";
			}
			if($uploaded_files[] = (isset($_FILES['images']['name']['1']))) {
				$img2 = $uploaded_files[1];
			} else {
				$img2 = "";
			}
			if($uploaded_files[] = (isset($_FILES['images']['name']['2']))) {
				$img3 = $uploaded_files[2];
			} else {
				$img3 ="";
			}
			if($uploaded_files[] = (isset($_FILES['images']['name']['3']))) {
				$img4 = $uploaded_files[3];
			} else {
				$img4 ="";
			}
			if($uploaded_files[] = (isset($_FILES['images']['name']['4']))) {
				$img5 = $uploaded_files[4];
			} else {
				$img5="";
			}
			
			$this->form_validation->set_rules('comment_id','Coment ID','required');
			$this->form_validation->set_rules('ticked_id','Ticked ID','required');
			$this->form_validation->set_rules('comments','comments','required');
			$this->form_validation->set_rules('datetime','datetime','required');
			if($this->form_validation->run() == TRUE) {
			$insert_coments = array('comment_id' => $this->input->post('comment_id'),
										'ticked_id' => $this->input->post('ticked_id'),
										'comments' => $this->input->post('comments'),
										'datetime' => $this->input->post('datetime'),
										'images1' =>$img1,
										'images2' =>$img2,
										'images3' =>$img3,
										'images4' =>$img4,
										'images5' =>$img5);
			//insert db
			$this->Ticketmodel->insert_comments($insert_coments);
			
			/* Insert Log
			------------------------------------------------------------------------------*/
			if(getenv("HTTP_CLIENT_IP")) { 
				$ipad = getenv("HTTP_CLIENT_IP"); 
			} elseif(getenv("HTTP_X_FORWARDED_FOR")) { 
				$ipad = getenv("HTTP_X_FORWARDED_FOR"); 
			} else { 
				$ipad = getenv("REMOTE_ADDR"); 
			}
				$ipad = substr($ipad, 0,14);
				$translog_id = $this->TransLog->show_id_trans_log()->row();
		
				$this->xd = $translog_id->id + 1;
		
		
				$this->send_id =$_SESSION['name'];
				$this->tgl = date('Y-m-d H:i:s');
				
				$insert_log = array('id' => $this->xd,
							'user_id' =>$this->send_id,
							'ticket_id' =>$this->input->post(''),
							'notes' =>('Create Coments'),
							'datetime' =>$this->tgl,
							'ip' =>$ipad);
				//insert ke table trans log
				$this->TransLog->insert_log($insert_log);
			/* end Trnslog
			--------------------------------------------------------------------------------*/
			
			echo "<script>window.close();opener.location.reload();</script>";
			} else {
				$this->session->set_flashdata('message', 'Create Comments Not Empty... !');
				echo "<script>window.close();opener.location.reload();</script>";
			}
		}
/* END CREATE COMENTS
--------------------------------------------------------------------------------------------------*/
			$this->load->vars($this->data);
			$this->load->view('layout/head');
			$this->load->view('layout/body');
			$this->load->view('layout/slice-atas');
			$this->load->view('ticket/ticket2');
			$this->load->view('layout/slice_bawah');
		
		

	}
	var $limit =5;
	function search_reference($id){
		$offset = 0;
		
		//$this->output->enable_profiler(true);
		//offset
		$uri_segment = 5;
		$offset = $this->uri->segment($uri_segment);
		//die($offset);
		$num_rows = $this->Ticketmodel->count_all($id);
		if($num_rows > 0) {
		
		$config['base_url'] = site_url('createticket/ticket/search_reference/'.$id);
		$config['total_rows'] = $num_rows;
		$config['per_page'] = $this->limit;
		$config['uri_segment'] = $uri_segment;
		$this->pagination->initialize($config);
		$this->data['pagination'] = $this->pagination->create_links();
		$this->data['search'] = $this->Ticketmodel->search_reference($id,$offset);
		
		}
		
		$this->load->vars($this->data);
		$this->load->view('ticket/search_reference');	
	}
	function coments() {
		$this->data['idcomnt'] = $this->Ticketmodel->show_id_comments();
		$this->data['id_ticket'] = $this->Ticketmodel->show_id();
		$this->load->vars($this->data);
		$this->load->view('ticket/coments');
	}
	
	function PerPage()
	{
		return 5;
	}

	
	
}
?>